#pragma once
#include<iostream>
#include"channel.hpp"
#include"epoller.hpp"
#include"listener.hpp"

//管理器，用于管理监听器和连接
#define TIMEOUT 1000000//毫秒
class Reactor{
    public:
        Reactor():_epoller(new Epoller){
        }
        ~Reactor(){
            for(auto e:_channels){
                delete e.second;
            }
            for(auto e:_clients){
                delete e.second;
            }
            delete _epoller;
        }
        //添加监听事件
        bool AddChannel(int fd,Channel* channel,epoll_event* event,InetAddr* client_addr){
            if(_epoller->AddChannel(fd,event)){
                _channels[fd]=channel;
                _clients[fd]=client_addr;
                LOG(ly::LogLevel::INFO)<<"添加监听事件成功"<<"\n";
                return true;
            }
            else{
                LOG(ly::LogLevel::ERROR)<<"添加监听事件失败"<<"\n";
                return false;
            }
        }
        //删除监听事件
        bool DelChannel(int fd){
            if(_channels.find(fd)!=_channels.end()){
                delete _channels[fd];
                _channels.erase(fd);
                delete _clients[fd];
                _clients.erase(fd);
                if(_epoller->DelChannel(fd)){
                    return true;
                }
                LOG(ly::LogLevel::ERROR)<<"删除监听事件失败"<<"\n";
                return false;
            }
            else{
                return false;
            }
        }
        //修改监听事件
        bool ModChannel(int fd,epoll_event* event){
            if(_channels.find(fd)!=_channels.end()){
                return _epoller->ModChannel(fd,event);
            }
            else{
                return false;
            }
        }
        void ErrorHandling(){
            
        }
        void Wait(int timeout=TIMEOUT){
            epoll_event events[1024];
            int n=epoll_wait(_epoller->GetFd(),events,1024,timeout);
            if(n==-1){
                LOG(ly::LogLevel::ERROR)<<"epoll_wait失败"<<"\n";
                return;
            }
            //LOG(ly::LogLevel::INFO)<<"收到"<<n<<"个事件"<<"\n";
            for(int i=0;i<n;i++){
                int fd=events[i].data.fd;
                if(_channels.find(fd)==_channels.end()){
                    LOG(ly::LogLevel::ERROR)<<"未找到对应的channel"<<"\n";
                    exit(1);
                }
                LOG(ly::LogLevel::INFO)<<"收到事件:"<<fd<<"\n";
                Channel* channel=_channels[fd];
                InetAddr* client_addr=_clients[fd];
                if(channel==nullptr || client_addr==nullptr){
                    LOG(ly::LogLevel::ERROR)<<"未找到对应的channel"<<"\n";
                }
                if(events[i].events&EPOLLIN){
                    int n=channel->Recv();
                    LOG(ly::LogLevel::INFO)<<"收到数据"<<client_addr->PrintDebug()<<"\n";
                    if(n<0){
                        //集中处理错误

                        LOG(ly::LogLevel::ERROR)<<"接收失败"<<"\n";
                    }
                    else if(n==0){
                        //对端关闭
                        DelChannel(fd);
                    }
                }
                // if(events[i].events&EPOLLOUT){
                //     channel->Send();
                // }
            }
        }
        void Run(){
            _running=true;
            while(_running){
                Wait();
            }
        }
        void Stop(){
            _running=false;
        }
    private:
    bool _running;
    Epoller* _epoller;
    std::map<int,Channel*> _channels;
    std::map<int,InetAddr*> _clients;//连接的信息
    //std::map<int,Connection*> _connections;
};